package com.nine.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nine.dao.AccountMapper;
import com.nine.entity.Account;
import com.nine.service.AccountService;



import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


import com.nine.dao.BillsMapper;
import com.nine.dao.UsersMapper;
import com.nine.entity.Bills;
import com.nine.entity.Users;
import com.nine.entity.UsersWithBLOBs;

/**
 * 银行账户业务实现层
 * 
 * @author hubing
 *
 */
@Service
@Transactional
public class AccountServiceImpl implements AccountService {
	@Autowired
	AccountMapper accountMapper;
	@Autowired
	UsersMapper usersMapper;
	@Autowired
	BillsMapper billsMapper;

	// 修改金额
	@Override
	public int updateMoneyByAccountid(Account account) {
		return accountMapper.updateMoneyByAccountid(account);
	}

	// 查单条银行账户表记录
	@Override
	public Account selectAccount(Integer accountid) {
		return accountMapper.selectByPrimaryKey(accountid);
	}

	// 获取20位订单号
	@Override
	public String getUUID() {
		String uuid;
		String bid;
		int temp = 0;
		do {
			uuid = UUID.randomUUID().toString();
			String uuidArray[] = uuid.split("-");
			SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
			bid = sdf.format(new Date()) + uuidArray[0];
			temp = accountMapper.selectUUID(bid);
		} while (temp == 1);
		return bid;
	}

	// 添加订单表记录
	@Override
	public int insertBills(Bills bills) {
		return accountMapper.insertBills(bills);
	}

	// 汇款充值 保存临时（状态为3）订单到数据库,并返回订单号
	@Override
	public String paymentOfRemittances(int id, float moneyin, String keywords) {
		UsersWithBLOBs user = usersMapper.selectByPrimaryKey(id);
		Bills bills = new Bills();
		bills.setBid(getUUID());
		bills.setUid(user.getUsername());
		bills.setMoneyin(moneyin);
		bills.setDate(new Date());
		bills.setKeywords(keywords);
		Account account = accountMapper.selectByPrimaryKey(Integer.parseInt(user.getAccountid()));
		int moneynow = (int) (moneyin + account.getMoney());
		bills.setMoneynow(moneynow);
		bills.setState(3);
		accountMapper.insertBills(bills);
		return bills.getBid();
	}

	// 修改临时订单状态为已保存
	@Override
	public int updateBillsState(String bid) {
		return accountMapper.updateBillsState(bid);
	}

	// 通过订单号，修改支付成功订单的订单状态和银行账户金额
	@Override
	public int saveBillsStateAndAccountMoney(String bid) {
		Bills bills = accountMapper.selectBills(bid);
		UsersWithBLOBs user = accountMapper.selectUserByName(bills.getUid());
		Account account = new Account();
		account.setAccountid(Integer.parseInt(user.getAccountid()));
		account.setMoney(bills.getMoneyin());

		Account accountTemp = accountMapper.selectByPrimaryKey(account.getAccountid());
		bills.setBid(bid);
		bills.setMoneynow((int) (float) (bills.getMoneyin() + accountTemp.getMoney()));
		int moneynowTemp = accountMapper.updateBillsMoneynow(bills);
		int stateTemp = accountMapper.updateBillsState(bid);
		int monryTemp = accountMapper.updateMoneyByAccountid(account);
		if (stateTemp == 1 && monryTemp == 1 && moneynowTemp == 1)
			return 1;
		else
			return 0;
	}
	// 重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写
	// 重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写
	// 重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写
	// 重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写
	// 重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写
	// 重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写重新编写

	// 通过登录用户ID修改money，并添加汇款记录
	@Override
	@Transactional
	public int bankHuiKuanChongZhi(int id, float money, String keywords) {
		String accountid = accountMapper.bank1SelectAccountid(id);
		Map<String, Object> map = new HashMap<>();
		map.put("accountid", Integer.parseInt(accountid));
		map.put("money", money);
		int updateMoney = accountMapper.bank1UpdateMoney(map);

		Bills bills = new Bills();
		bills.setBid(bankGetCZUUID());
		Map<String, Object> map1 = accountMapper.bank1SelectUsernameOfMoneyByID(id);
		bills.setUid(((String) map1.get("username")));
		bills.setMoneyin(money);
		bills.setDate(new Date());
		bills.setKeywords(keywords);
		float mon = (float) map1.get("money");
		bills.setMoneynow((int) mon);
		bills.setState(1);
		int insertBills = billsMapper.insertSelective(bills);
		if (updateMoney == 1 && insertBills == 1) {
			return 1;
		} else {
			return 0;
		}

	}

	// 获得充值订单编号
	@Override
	public String bankGetCZUUID() {
		String uuid;
		String bid;
		int temp = 0;
		do {
			uuid = UUID.randomUUID().toString();
			String uuidArray[] = uuid.split("-");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			bid = "CZ" + sdf.format(new Date()) + uuidArray[1];
			temp = accountMapper.bank1SelectBillsBid(bid);
		} while (temp == 1);
		return bid;
	}

	// 验证登录用户的支付密码
	@Override
	public int bank1SelectPaycodeById(int id, int paycode) {
		Map<String, Object> map = new HashMap<>();
		map.put("id", id);
		map.put("paycode", paycode);
		return accountMapper.bank1SelectPaycodeById(map);
	}

	// 查询登录用户所有充值汇款信息
	@Override
	public List<Bills> bank1SelectBillsByUsernameAndCZ(String username) {
		return accountMapper.bank1SelectBillsByUsernameAndCZ(username);
	}

	// 验证密码
	@Override
	public int bank1SelectPaycode(int id, int paycode) {
		Map<String, Object> map = new HashMap<>();
		map.put("id", id);
		map.put("paycode", paycode);
		return accountMapper.bank1SelectPaycode(map);
	}
	//获取转账订单号
	@Override
	public Map<String, String> bankGetZZUUID() {
		Map<String, String> map=new HashMap<>();
		String uuid;
		String bid;
		int temp = 0;
		do {
			uuid = UUID.randomUUID().toString();
			String uuidArray[] = uuid.split("-");
			SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
			bid = "ZZAA" + sdf.format(new Date()) + uuidArray[1];
			temp = accountMapper.bank1SelectBillsBid(bid);
		} while (temp == 1);
		map.put("AA",bid);
		String bidBB="ZZBB"+bid.substring(4, bid.length());
		map.put("BB", bidBB);
		return map;
	}

	// 验证密码成功，转账操作数据库
	@Override
	@Transactional
	public void bank1NeiBuZhuanZhang(int id, String rid, float money) {
		Map<String, Object> map = new HashMap<>();
		map.put("id", id);
		map.put("money", -money);
		accountMapper.bank1UpdateMoneyZZ(map);
		
		map.put("id", accountMapper.bank1SelectUserIdByUsername(rid));
		map.put("money", money);
		accountMapper.bank1UpdateMoneyZZ(map);
		
		Map<String, String> uuidMap=bankGetZZUUID();//获取订单号
		Date date=new Date();//获取当前系统时间
		
		Bills biu=new Bills();
		biu.setBid(uuidMap.get("AA"));
		String uid=accountMapper.bank1SelectUsernameByID(id);
		biu.setUid(uid);
		biu.setRid(rid);
		biu.setMoneyout(money);
		biu.setDate(date);
		biu.setKeywords("转账支出");
		float moneyU=accountMapper.bank1selectMoneyByUsername(uid);
		biu.setMoneynow((int)moneyU);
		biu.setState(1);
		
		Bills bir=new Bills();
		bir.setBid(uuidMap.get("BB"));
		bir.setUid(uid);
		bir.setRid(rid);
		bir.setMoneyin(money);
		bir.setDate(date);
		bir.setKeywords("转账收入");
		float moneyR=accountMapper.bank1selectMoneyByUsername(rid);
		bir.setMoneynow((int)moneyR);
		bir.setState(1);
		
		billsMapper.insertSelective(biu);
		billsMapper.insertSelective(bir);
		
		
	}
	
	// 查询登录用户所有充值汇款信息
	@Override
	public List<Bills> bank1SelectBillsByUsernameAndZZ(String username) {
		return accountMapper.bank1SelectBillsByUsernameAndZZ(username);
	}
	
	@Override
	public int insertSelective(Account record) {
		// TODO Auto-generated method stub
		return accountMapper.insertSelective(record);
	}
	
	//根据用户名获取支付密码
	@Override
	public String getpayCodeByUname(String username) {
		// TODO Auto-generated method stub
		return accountMapper.getpayCodeByUname(username);
	}

	
	//根据用户名来修改密码
	@Override
	public Integer updatePayCodeByUsername(String username, String paycode) {
		// TODO Auto-generated method stub
		return accountMapper.updatePayCodeByUsername(username, paycode);
	}

}
